
import * as Constant from '../constant'
import * as OrderService from '../service/order'
import * as Utils from '../utils'

export default {
    namespace: 'order',
    state: {
        order: {},
        list: [],
        map: {}
    },
    reducers: {
        setOrder(state, { payload }) {
            return {
                ...state,
                order: payload
            }
        },
        order(state, { payload }) {
            let list = []
            let map = {}
            if (Array.isArray(payload)) {
                payload.forEach(item => {
                    list.push(item)
                    map[item._id] = item
                })
            }
            return {
                ...state,
                list, 
                map
            }
        }
    },
    effects: {
        *createrOrder({ payload }, { put, select }) {
            let state = yield select()
            let order = {}
            let openid = state.user.user.openid
            let commodityList = []
            let status = Constant.ORDER_STATUS.ORDER_STATUS_REDAY
            let address = {}
            let money = 0
            try {
                address = {...state.user.addressList.list[0]}

                if (payload === undefined) {
                    // 1. 从购物车创建订单
                    for (let commodity of state.cart.cart.list) {
                        if (commodity.checked) {
                            commodityList.push(commodity)
                        }
                    }
                    money = state.cart.total.money
                } else {
                    // 2. 商品详情页创建订单
                    payload.count = 1
                    commodityList = [payload]
                    money = payload.price
                }

                order = {
                    openid, commodityList, status,
                    address, money
                }
                yield put({ type: 'setOrder', payload: order })
            } catch (error) {
                console.log(error)
            }
        },
        *putOrder({ payload, resolve, reject }, { put, call }) {
            let res = yield call(OrderService.create, payload)
            let params = {
                resolve, reject,
                order: res.data
            }
            yield put({ type: 'getOrderList', payload: params })
        },
        *getOrderList({ payload = {status: ''} }, { put, call, select }) {
            let state = yield select()
            try {
                let openid = state.user.user.openid
                let res = yield call(OrderService.find, openid, payload.status)
                yield put({ type: 'order', payload: res.data })
                if (payload.resolve) {
                    payload.resolve(payload.order._id)
                }
            } catch (error) {
                payload.reject(error)
            }
        },
        // 取消订单
        *cleanOrder({ payload }, { call, put }) {
            console.log('cleanOrder 2')
            payload.status = Constant.ORDER_STATUS.ORDER_STATUS_CLEAN
            yield call(OrderService.update, payload)
            yield put({ type: 'getOrderList' })
        },
        // 订单支付
        *pay({ payload }, { call, put, select }) {
            // 1. 调用支付接口
            // 2. 支付接口返回支付成功
            // 3. 修改订单状态为已支付 ?

            let state = yield select()
            try {
                let openid = state.user.user.openid
                let res = yield call(OrderService.pay, openid)
                let wxPayRes = yield Utils.wxPay(res.data)
                if (wxPayRes) {
                    // 3. 修改订单状态为已支付
                    payload.status = Constant.ORDER_STATUS.ORDER_STATUS_PAY
                    yield call(OrderService.update, payload)
                    yield put({ type: 'getOrderList' })
                }
            } catch (error) {
                
            }
        }
    }
}
